#include <my_head.h>

// 定义无名信号量
sem_t sem1;
sem_t sem2;
sem_t sem3;

void *task_func_1(void *arg){
    while(1){
        sem_wait(&sem1);
        printf("A");
        fflush(stdout);
        sleep(1);
        sem_post(&sem2);
    }
}

void *task_func_2(void *arg){
    while(1){
        sem_wait(&sem2);
        printf("B");
        fflush(stdout);
        sleep(1);
        sem_post(&sem3);
    }
}

int main(int argc, const char *argv[]){
    // 初始化无名信号量
    sem_init(&sem1, 0, 1);
    sem_init(&sem2, 0, 0);
    sem_init(&sem3, 0, 0);
    pthread_t tid1, tid2;
    int ret = 0;
    if(0 != (ret = pthread_create(&tid1, NULL, task_func_1, NULL))){
        printf("pthread_create error : [%d] [%s]\n", ret, strerror(ret));
        exit(EXIT_FAILURE);
    }
    if(0 != (ret = pthread_create(&tid2, NULL, task_func_2, NULL))){
        printf("pthread_create error : [%d] [%s]\n", ret, strerror(ret));
        exit(EXIT_FAILURE);
    }
    while(1){
        sem_wait(&sem3);
        printf("C");
        fflush(stdout);
        sleep(1);
        sem_post(&sem1);
    }
    // 销毁无名信号量
    sem_destroy(&sem1);
    sem_destroy(&sem2);
    sem_destroy(&sem3);
    return 0;
}